/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2013-2014 sagyf Yang. The Four Group.
 */

package app.kits;

import com.github.sog.config.StringPool;
import com.github.sog.kit.Tuple;
import com.github.sog.kit.io.ResourceKit;
import com.github.sog.libs.AppFunc;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import com.jfinal.kit.PathKit;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import static com.google.common.primitives.Ints.tryParse;

/**
 * <p>
 * 停车位导航.
 * </p>
 *
 * @author sagyf yang
 * @version 1.0 2014-04-26 13:51
 * @since JDK 1.6
 */
public class ParkingNavKit {

    public static final  String                PIXEL            = "PIXEL.";
    private static final Map<String, byte[][]> FLOOR_MAP_POINTS = Maps.newHashMap();
    private static final Map<String, int[]>    FLOOR_PIXEL_SIZE = Maps.newHashMap();
    private static final Logger                logger           = LoggerFactory.getLogger(ParkingNavKit.class);
    private static BufferedImage start_image;
    private static BufferedImage end_image;

    public static void init() {
        Map<String, String> config_data = ResourceKit.readProperties("floor-map.conf");
        Map<String, String> map_conf = Maps.newHashMap();
        for (String floor : config_data.keySet()) {
            final String line_data = config_data.get(floor);
            if (Strings.isNullOrEmpty(line_data) || StringUtils.equals(StringPool.ZERO, line_data)) {
                continue;
            }
            if (StringUtils.startsWith(floor, PIXEL)) {
                //像素
                final List<String> pixel = AppFunc.COMMA_SPLITTER.splitToList(line_data);
                final Integer first = tryParse(pixel.get(0));
                final Integer second = tryParse(pixel.get(1));
                byte[][] floor_pixel = new byte[first][second];
                final String floor_mark = StringUtils.replace(floor, PIXEL, StringPool.EMPTY);
                FLOOR_MAP_POINTS.put(floor_mark, floor_pixel);
                FLOOR_PIXEL_SIZE.put(floor_mark, new int[]{first, second});
            } else {
                map_conf.put(floor, line_data);
            }
        }

        for (String floor : map_conf.keySet()) {
            String map_data = map_conf.get(floor);
            byte[][] floor_pixel = FLOOR_MAP_POINTS.get(floor);
            final String[] split = map_data.split("\\|");
            for (int i = 0; i < split.length; i++) {

                final String[] split1 = split[i].split(",");
                for (int j = 0; j < split1.length; j++) {
                    int value = Integer.parseInt(split1[j]);
                    floor_pixel[i][j] = (byte) value;
                }
            }
        }
        try {
            final String clsPath = PathKit.getRootClassPath();
            start_image = ImageIO.read(new File(clsPath + "/img/start.png".replace(StringPool.SLASH, File.separator)));
            end_image = ImageIO.read(new File(clsPath + "/img/end.png".replace(StringPool.SLASH, File.separator)));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static List<Tuple<Integer, Integer>> path(String floor, int start_x, int start_y, int end_x, int end_y) {
        ParkingMapKit parkingMapKit = StringUtils.equals("B2", floor) ? ParkingMapKit.createB2() : ParkingMapKit.create();
        byte[][] map_points = FLOOR_MAP_POINTS.get(floor);
        int[] size = FLOOR_PIXEL_SIZE.get(floor);
        parkingMapKit.setMap(map_points, size[1], size[0]);
        return parkingMapKit.getPath(start_x, start_y, end_x, end_y);
    }


    public static void build_road(String image_path, String dest_path, List<Tuple<Integer, Integer>> points) {
        try {
            BufferedImage buffer = ImageIO.read(new File(image_path));
            Graphics2D g = (Graphics2D) buffer.getGraphics();
            g.setColor(Color.red);
            g.setStroke(new BasicStroke(3.8f));
            int point_size = points.size();
            final int last_idx = point_size - 1;
            for (int i = 0; i < point_size; i++) {
                final Tuple<Integer, Integer> point = points.get(i);
                if (i == 0) {
                    g.drawImage(start_image, point.x - 10, point.y - 15, null);
                }
                if (i != last_idx) {
                    final Tuple<Integer, Integer> next_point = points.get(i + 1);
                    g.drawLine(point.x, point.y, next_point.x, next_point.y);
                }
            }
            Tuple<Integer, Integer> end_point = points.get(last_idx);
            g.drawImage(end_image, end_point.x - 10, end_point.y - 10, null);
            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream(dest_path);
                ImageIO.write(buffer, "jpg", fos);
            } finally {
                IOUtils.closeQuietly(fos);
                g = null;
            }
        } catch (IOException e) {
            logger.error("create template image is error!", e);
        }
    }
}
